package de.zainodis.triplog.ui;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;

import de.zainodis.triplog.R;
import de.zainodis.triplog.data.DataProvider;
import de.zainodis.triplog.ui.fragment.IForecastList;
import de.zainodis.triplog.ui.fragment.WeatherForecastDetailFragment;
import de.zainodis.triplog.ui.task.IWeatherForecastActions;


public class WeatherForecastActivity extends Activity implements IWeatherForecastActions {

    private static final String TAG = "MainActivity";
    private static final String DETAIL_TAG = "WeatherForecastDetailFragment";
    private boolean isDualPane = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather_forecast);
        PreferenceManager.setDefaultValues(this, R.xml.preferences, false);

        if (findViewById(R.id.weather_detail_container) != null) {
            isDualPane = true;

            if (savedInstanceState == null) {
                Fragment fragment = new WeatherForecastDetailFragment();
                FragmentManager manager = getFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.replace(R.id.weather_detail_container, fragment, DETAIL_TAG);
                transaction.commit();
            }
        }

        IForecastList forecastList = (IForecastList) getFragmentManager().findFragmentById(R.id.fragment_forecast);
        forecastList.setIsDualPane(isDualPane);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            startActivity(new Intent(this, SettingsActivity.class));
            return true;
        } else if (id == R.id.action_map) {
            //If we got a current location take that, otherwise use the location of the trip
            String mapRequestUri = "http://maps.google.co.in/maps?q=" + DataProvider.getCurrentTrip().getLocation().getLocationSetting();
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(mapRequestUri));
            //So we return to our app if back is pressed
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public void displayForecastDetails(int databaseRowId) {
        if (isDualPane) {
            //Update the detail view
            WeatherForecastDetailFragment fragment = new WeatherForecastDetailFragment();
            Bundle bundle = new Bundle();
            bundle.putInt(WeatherForecastDetailFragment.WEATHER_REPORT_ID, databaseRowId);
            fragment.setArguments(bundle);
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.weather_detail_container, fragment, DETAIL_TAG);
            transaction.commit();

        } else {
            Intent intent = new Intent(this, DetailActivity.class);
            intent.putExtra(WeatherForecastDetailFragment.WEATHER_REPORT_ID, databaseRowId);
            startActivity(intent);
        }
    }
}
